using System;
using System.Collections;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

    public partial class _Default : System.Web.UI.Page
    {
        //Results User Control
        Results results;

        //Edit User Control
        Edit edit;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //If it is first time to page populate the drop downs
            if (!IsPostBack)
            {
                PopulateTimeSpanDropDown();
                PopulateLoadTestDropdown(GetDropdownValue());
                
                
            }

            //if a run id exists, redirect to the edit page
            string runID = (string)Session["RunId"];
            if (runID != null && runID.Length > 0)
            {
                LoaduserControl("Edit.ascx");
                this.ddLoadTests.Enabled = false;
                this.ddTimeSpan.Enabled = false;
            }
            else
            {
                //If RunId was not in the session then load the results control
                LoaduserControl("Results.ascx");
                this.ddLoadTests.Enabled = true;
                this.ddTimeSpan.Enabled = true;

            
            }

            

        }

        private void PopulateTimeSpanDropDown()
        {
            if (IsPostBack) { return; }

            //TODO: create a datasource
            ddTimeSpan.Items.Clear();
            ddTimeSpan.Items.Add("All");
            ddTimeSpan.Items.Add("Today");
            ddTimeSpan.Items.Add("5");
            ddTimeSpan.Items.Add("10");
            ddTimeSpan.Items.Add("30");

            //default dropdwon to previous selection
            if (Session["pastDays"] != null)
            {
                int pastDays = (int)Session["pastDays"];
                if (pastDays > 0)
                {
                    ddTimeSpan.Items.FindByValue(pastDays.ToString()).Selected = true;
                }
                else if (pastDays == 0)
                {
                    ddTimeSpan.Items.FindByValue("Today").Selected = true;
                }
                else
                {
                    ddTimeSpan.Items.FindByValue("All").Selected = true;
                }
                
            }

            
        }

        private void PopulateLoadTestDropdown(int DaysBefore)
        {
            //load test from the database
            DBHelper dbHelper = new DBHelper();
            ArrayList loadTests = dbHelper.GetLoadTests(DaysBefore);
            


            //bind results to the drop down
            this.ddLoadTests.DataSource = loadTests;
            this.ddLoadTests.DataBind();

            //set load test to the previous selected load test if it still exists
            if (Session["LoadTest"] != null)
            {
                string test = (string)Session["LoadTest"];

                try
                {
                    ddLoadTests.Items.FindByValue(test).Selected = true;
                }
                catch
                {
                    //ignore exception.  If test does not exist default to first item
                }
            }
            else
            {
                ddLoadTests.SelectedIndex = 0;
            }
            
            
            

        }

        private void LoaduserControl(string controlPage)
        {
            switch (controlPage)
            {
                case "Results.ascx":
                    results = (Results)Page.LoadControl(controlPage);
                    PlaceHolder1.Controls.Add(results);
                    
                    //results.ParentPage = this;
                    Repopulate();
                    break;
                case "Edit.ascx":
                    edit = (Edit)Page.LoadControl(controlPage);
                    PlaceHolder1.Controls.Clear();
                    PlaceHolder1.Controls.Add(edit);
                    break;
            }

        }

        public void Repopulate()
        {
            foreach (Control C in PlaceHolder1.Controls)
            {
                try
                {
                    Session["LoadTest"] = this.ddLoadTests.SelectedValue;
                    Session["pastDays"] = GetDropdownValue();
                    ((Results)C).Populate();
                }
                catch
                {
                    //ignore when this is not Results control
                }
            }
        }

        private int GetDropdownValue()
        {
            if (ddTimeSpan.SelectedValue == "All")
                return -1;
            else if (ddTimeSpan.SelectedValue == "Today")
                return 0;
            else return int.Parse(ddTimeSpan.SelectedValue);
        }


        protected void ddLoadTests_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            //set the grid view back to the first page
            Session["CurrentPageIndex"] = 0;
            
            //get values from drop down and add to session
            Session["LoadTest"] = this.ddLoadTests.SelectedValue;
            Session["pastDays"] = GetDropdownValue();

            //populate the results control with data from the new load test
            Results results= (Results)PlaceHolder1.Controls[0];
            if (results != null)
                results.Populate();                                
        }
        
        protected void ddTimeSpan_SelectedIndexChanged(object sender, EventArgs e)
        {
            //set the number of days to search for results
            Session["pastDays"] = GetDropdownValue();

            //Update load test drop down to only have tests in the day range
            PopulateLoadTestDropdown(GetDropdownValue());
            
            //populate the results grid
            Repopulate();
        }
        protected void btnDeleteRuns_Click(object sender, EventArgs e)
        {
            Response.Redirect("Delete.aspx", false);
        }
}

